/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import DAO.NhanVienDao;
import DAO.KetNoi;
import GUI.TOOL.CreateDatabase;
import POJO.NhanVien;
import java.awt.Dimension;
import java.sql.SQLException;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author Khôi
 */
public class Login extends javax.swing.JFrame {

	/**
	 * Creates new form Login
	 */
	public Login() {
		initComponents();
		this.setLocation(400, 250);
		anNutTaoCSDL();
		KetNoi kn = null;
		try {
			KetNoi.setUrl(KetNoi.docCauHinh());
			kn = new KetNoi();

		} catch (SQLException ex) {
			JOptionPane.showMessageDialog(this, "Không thể kết nối CSDL", "Thông báo lỗi", JOptionPane.ERROR_MESSAGE);

			System.out.println("Không thể restore CSDL");
			hienNutTaoCSDL();
			return;

		} catch (Exception e) {

			return;
		}
	}

	/**
	 * Tài khoản mặc định để đăng nhập user: sa pass:sa
	 */
	/**
	 * This method is called from within the constructor to initialize the
	 * form. WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
       // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
       private void initComponents() {

              jPanel1 = new javax.swing.JPanel();
              jLabel1 = new javax.swing.JLabel();
              txPass = new javax.swing.JPasswordField();
              jLabel2 = new javax.swing.JLabel();
              txUser = new javax.swing.JTextField();
              btLogin = new javax.swing.JButton();
              jButton1 = new javax.swing.JButton();
              btTaoCSDL = new javax.swing.JButton();
              jPanel2 = new javax.swing.JPanel();
              jLabel5 = new javax.swing.JLabel();

              setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
              setResizable(false);

              jLabel1.setText("Ussername:");

              jLabel2.setText("Password:");

              btLogin.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
              btLogin.setText("Log in");
              btLogin.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            btLoginActionPerformed(evt);
                     }
              });

              jButton1.setText("Help");
              jButton1.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            jButton1ActionPerformed(evt);
                     }
              });

              btTaoCSDL.setText("Tạo cơ sở dữ liệu");
              btTaoCSDL.addActionListener(new java.awt.event.ActionListener() {
                     public void actionPerformed(java.awt.event.ActionEvent evt) {
                            btTaoCSDLActionPerformed(evt);
                     }
              });

              javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
              jPanel1.setLayout(jPanel1Layout);
              jPanel1Layout.setHorizontalGroup(
                     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel1Layout.createSequentialGroup()
                            .addContainerGap(145, Short.MAX_VALUE)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                   .addComponent(jLabel2)
                                   .addComponent(jLabel1))
                            .addGap(30, 30, 30)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                   .addComponent(txPass, javax.swing.GroupLayout.DEFAULT_SIZE, 153, Short.MAX_VALUE)
                                   .addComponent(txUser))
                            .addContainerGap(65, Short.MAX_VALUE))
                     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btLogin)
                            .addGap(29, 29, 29))
                     .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jButton1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(btTaoCSDL)
                            .addGap(0, 0, Short.MAX_VALUE))
              );
              jPanel1Layout.setVerticalGroup(
                     jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel1Layout.createSequentialGroup()
                            .addContainerGap(13, Short.MAX_VALUE)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(jLabel1)
                                   .addComponent(txUser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(txPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                   .addComponent(jLabel2))
                            .addGap(18, 18, 18)
                            .addComponent(btLogin)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                   .addComponent(jButton1)
                                   .addComponent(btTaoCSDL))
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
              );

              getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

              jLabel5.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
              jLabel5.setText("Log in");

              javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
              jPanel2.setLayout(jPanel2Layout);
              jPanel2Layout.setHorizontalGroup(
                     jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                            .addContainerGap(205, Short.MAX_VALUE)
                            .addComponent(jLabel5)
                            .addContainerGap(181, Short.MAX_VALUE))
              );
              jPanel2Layout.setVerticalGroup(
                     jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel2Layout.createSequentialGroup()
                            .addGap(0, 0, 0)
                            .addComponent(jLabel5)
                            .addGap(0, 0, Short.MAX_VALUE))
              );

              getContentPane().add(jPanel2, java.awt.BorderLayout.PAGE_START);

              pack();
       }// </editor-fold>//GEN-END:initComponents

	private void anNutTaoCSDL() {
		btTaoCSDL.setVisible(false);
		btLogin.setEnabled(true);

	}

	private void hienNutTaoCSDL() {
		btTaoCSDL.setVisible(true);
		btLogin.setEnabled(false);
	}
       private void btLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btLoginActionPerformed
		// TODO add your handling code here:

		NhanVien nv = new NhanVien();
		NhanVien nv2 = new NhanVien();
		nv.setUserName(txUser.getText().trim());
		nv.setPassWord(txPass.getText().trim());

		if (txUser.getText().trim().equals("sa") && txPass.getText().trim().equals("sa")) {
			//Var.NV = new NhanVien(nv2);
			Var.NV.setLoaiNV(10);
			MainForm fr = new MainForm();
			fr.setExtendedState(JFrame.MAXIMIZED_BOTH);
			fr.setVisible(true);
			this.setVisible(false);
			return;
		} else {

			NhanVienDao dao = new NhanVienDao();
			try {
				nv2 = dao.getByUsername(nv.getUserName());

			} catch (Exception e) {
			}

			if (nv2 != null && nv.getPassWord().equals(nv2.getPassWord()) && nv2.getTrangThai() == 1) {
				JOptionPane.showMessageDialog(this, "Đăng nhập thành công.");
				Var.NV = new NhanVien(nv2);
				MainForm fr = new MainForm();
				fr.setPreferredSize(new Dimension(600, 600));
				fr.setMinimumSize(new Dimension(600, 600));
				fr.setExtendedState(JFrame.MAXIMIZED_BOTH);

				fr.setVisible(true);
				this.setVisible(false);

			} else {
				JOptionPane.showMessageDialog(this, "Đăng nhập thất bại .");
			}
		}
       }//GEN-LAST:event_btLoginActionPerformed

       private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
		// TODO add your handling code here:
		About fr = new About();
		fr.setVisible(true);
       }//GEN-LAST:event_jButton1ActionPerformed

       private void btTaoCSDLActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btTaoCSDLActionPerformed
		// TODO add your handling code here:
		GUI.TOOL.CreateDatabase fr = new CreateDatabase();
		if (fr.tao()) {
			fr.dispose();
		} else {
			fr.setVisible(true);
		}
       }//GEN-LAST:event_btTaoCSDLActionPerformed

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		/* Set the Nimbus look and feel */
		//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
		 * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
		 */
		try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		}
		//</editor-fold>

		/* Create and display the form */
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new Login().setVisible(true);
			}
		});
	}
       // Variables declaration - do not modify//GEN-BEGIN:variables
       public static javax.swing.JButton btLogin;
       public static javax.swing.JButton btTaoCSDL;
       private javax.swing.JButton jButton1;
       private javax.swing.JLabel jLabel1;
       private javax.swing.JLabel jLabel2;
       private javax.swing.JLabel jLabel5;
       private javax.swing.JPanel jPanel1;
       private javax.swing.JPanel jPanel2;
       private javax.swing.JPasswordField txPass;
       private javax.swing.JTextField txUser;
       // End of variables declaration//GEN-END:variables
}
